<template>
  <el-form  :inline="true" style="height: 30px">
    <el-form-item label="所属考核计划"  label-width="120px" >
      <el-select @change="doQueryByAssess()" v-model="queryParams.assessmentId"  placeholder="请选择"  size="small">
        <el-option
          v-for="item in AssessmentOptions"
          :key="item.value"
          :label="item.name"
          :value="item.value"
        ></el-option>
      </el-select>
    </el-form-item>
    <el-form-item label="所属考核方案" label-width="120px">
      <el-select style="width: 400px" @change="doQueryByProgramme()" v-model="queryParams.programmeId"  placeholder="请选择"  size="small">
        <el-option
          v-for="item in ProgrammeOptions"
          :key="item.value"
          :label="item.name"
          :value="item.value"
        ></el-option>
      </el-select>
    </el-form-item>
    <el-form-item label="所属部门" label-width="120px">
      <el-select style="width: 200px" @change="doQueryByDept()" v-model="queryParams.deptId"  placeholder="请选择"   size="small">
        <el-option
          v-for="item in deptOptions"
          :key="item.value"
          :label="item.name"
          :value="item.value"
        ></el-option>
      </el-select>
    </el-form-item>
    <el-form-item>
<!--      <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>-->
<!--      <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>-->
      <el-button type="warning" icon="el-icon-download" size="mini" @click="doExport">导出</el-button>
    </el-form-item>
  </el-form>
</template>

<script>
  import request from '@/utils/request'
  export default {
    name: 'statisticQueryForm',
    props: {
      pattern: undefined,
      deptType: undefined,
      dataUri: undefined,
      exportUri: undefined
    },
    data() {
      return{
        queryParams: {
          deptId: undefined,
          assessmentId: undefined,
          programmeId: undefined,
        },
        AssessmentOptions:[],
        ProgrammeOptions:[],
        deptOptions:[],
        curAssess: {},
        curProgramme: {},
        curDept: {}
      }
    },
    created() {
      this.getStaticQueryOption();
    },
    methods: {
      //查询表头
      getStaticQueryOption() {
        this.loading = true;
        request({
          url: '/kh/statistic/queryOptions' + '' + '?pattern=' + this.pattern + '&&deptType=' + this.deptType,
          method: 'get',
        }).then(response => {
          this.loading = false
          this.AssessmentOptions = response.data;
          this.doQueryByAssess();
        });
      },
      doQueryByAssess(){
        if(this.queryParams.assessmentId === undefined){
          this.queryParams.assessmentId = this.AssessmentOptions[0].value;
        }
        var curAssess =  this.AssessmentOptions.find(item => item.value === this.queryParams.assessmentId);
        this.ProgrammeOptions = curAssess.programmes;
        if(this.ProgrammeOptions && this.ProgrammeOptions.length>0){
          this.queryParams.programmeId = this.ProgrammeOptions[0].value;
        } else {
          this.queryParams.programmeId = undefined;
        }
        this.deptOptions = curAssess.depts;
        if(this.deptOptions && this.deptOptions.length> 0){
          this.queryParams.deptId = this.deptOptions[0].value;
        } else {
          this.queryParams.deptId = undefined;
        }
        this.doQuery();

      },
      doQueryByProgramme(){
        if(this.queryParams.assessmentId === undefined){
          this.queryParams.assessmentId = this.AssessmentOptions[0].value;
        }
        var curAssess =  this.AssessmentOptions.find(item => item.value === this.queryParams.assessmentId);

        if(this.queryParams.programmeId === undefined){
          this.ProgrammeOptions = curAssess.programmes;
          if(this.ProgrammeOptions && this.ProgrammeOptions.length>0){
            this.queryParams.programmeId = this.ProgrammeOptions[0].value;
          }
        }

        this.deptOptions = curAssess.depts;
        if(this.queryParams.deptId === undefined){
          if(this.deptOptions && this.deptOptions.length> 0){
            this.queryParams.deptId = this.deptOptions[0].value;
          }
        }
        this.doQuery();
      },
      doQueryByDept(){
        if(this.queryParams.assessmentId === undefined){
          this.queryParams.assessmentId = this.AssessmentOptions[0].value;
        }
        var curAssess =  this.AssessmentOptions.find(item => item.value === this.queryParams.assessmentId);

        if(this.queryParams.programmeId === undefined){
          this.ProgrammeOptions = curAssess.programmes;
          if(this.ProgrammeOptions && this.ProgrammeOptions.length>0){
            this.queryParams.programmeId = this.ProgrammeOptions[0].value;
          }
        }

        this.deptOptions = curAssess.depts;
        if(this.queryParams.deptId === undefined){
          if(this.deptOptions && this.deptOptions.length> 0){
            this.queryParams.deptId = this.deptOptions[0].value;
          }
        }
        this.doQuery();

      },
      doQuery(){
        if(!this.queryParams.assessmentId){
          this.$message.info("无考核计划");
          return;
        }
        if(!this.queryParams.programmeId){
          this.$message.info("考核计划不存在可统计的方案");
          return;
        }
        if(!this.queryParams.deptId){
          this.$message.info("未选择所属部门");
          return;
        }
        var obj = {};
        let requestUrl = this.dataUri +parseInt(this.queryParams.programmeId)+'?assessmentId='+parseInt(this.queryParams.assessmentId)+'&&deptId='+parseInt(this.queryParams.deptId);
        obj.url = requestUrl;
        this.$emit("doQuery", obj)
      },
      doExport() {
        if(!this.queryParams.assessmentId){
          this.$message.info("无考核计划");
          return;
        }
        if(!this.queryParams.programmeId){
          this.$message.info("考核计划不存在可统计的方案");
          return;
        }
        if(!this.queryParams.deptId){
          this.$message.info("未选择所属部门");
          return;
        }
        var obj = {};
        let requestUrl = this.exportUri +parseInt(this.queryParams.programmeId)+'?assessmentId='+parseInt(this.queryParams.assessmentId)+'&&deptId='+parseInt(this.queryParams.deptId);
        obj.url = requestUrl;
        var that = this;
        this.$confirm('导出统计结果', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return request({
            url: obj.url,
            method: 'get'
          })
        }).then(response => {
          this.download(response.msg);
          this.$emit("doExport", obj)
        }).catch(function() {

        });
      }
    },
    }
</script>

<style scoped>

</style>
